package com.zaofan.mobile.control;

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.zaofan.bean.OrderInfo;
import com.zaofan.exception.ServiceException;
import com.zaofan.mobile.bean.*;
import com.zaofan.service.OrderInfoService;
import com.zaofan.service.remote.RemoteService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/2.
 */
@Controller
@RequestMapping("order")
public class OrderController {

	private RemoteService remoteService = new RemoteService();
	
    @RequestMapping("/orderList.do")
    public String orderList(HttpServletRequest request, ModelMap modelMap){
//        OrderInfoService orderInfoService = new OrderInfoService();
//        try {
//            Account account = (Account)request.getSession().getAttribute("userinfo");
////            String userId = account.getId()+"";
//            Integer begin = 0;
//            Integer end = 5;
////            try {
////                begin = Integer.valueOf(begins);
////                end = Integer.valueOf(ends);
////            } catch (Exception e) {
////                e.printStackTrace();
////                JsonUtils.writeListFailJson("分页参数错误!!", response);
////                return;
////            }
//
//            List<UserOrder> order = orderInfoService.findByAcccountId2(account.getId(), begin, end);
////            request.getSession().setAttribute("orders",order);
//            modelMap.put("orders",order);
            modelMap.put("title","我的订单");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return "/order/orderList";
    }

    @RequestMapping("/orderDetail.do")
    public String orderDetail(HttpServletRequest request,int id, ModelMap modelMap){
        OrderInfoService orderInfoService = new OrderInfoService();
        try {
            UserOrder order = orderInfoService.getOrderInfo(id);
            modelMap.put("order",order);
            modelMap.put("title","订单详情");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/order/orderDetail";
    }

    @RequestMapping("/orderPay.do")
    public String orderPay(HttpServletRequest request, PayRequest payRequest, ModelMap modelMap) throws Exception {
        Map<String,String> wxpay = (Map<String, String>) request.getSession().getAttribute("wxpay");
        Account account = (Account)request.getSession().getAttribute("userinfo");

        if (wxpay == null){
            String wxUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxf946190506655c2d" +
                    "&secret=25f4e517eeea25ea18e75cab31cc3b8b&code="+payRequest.getCode()+"&grant_type=authorization_code";
            wxpay = new GsonBuilder().create().fromJson(WebUtils.doHttpRequest(wxUrl),new TypeToken<Map<String,String>>(){}.getType());
            request.getSession().setAttribute("wxpay",wxpay);
        }

        System.out.println("wxpay:"+wxpay.toString());
        String paybenUrl = "http://m.52letsgo.cn/pay/payBean?" +
                "orderNo=" + payRequest.getState() +
                "&userId=" + account.getId() +
                "&wechatOpenid=" + wxpay.get("openid") +
                "&orderType=1" +
                "&platform=TOUCH";
//        System.out.println("getpaybean:"+paybenUrl);
        String paybeanres = WebUtils.doHttpRequest(paybenUrl);
        Map<String,Object> paybean = new GsonBuilder().create().fromJson(paybeanres,new TypeToken<Map<String,Object>>(){}.getType());
//        PayBean paybean = new GsonBuilder().create().fromJson(paybeanres,PayBean.class);
//        System.out.println("paybean:"+paybean.toString());
        modelMap.put("orderNo",payRequest.getState());
//        modelMap.put("wxpay",wxpay);
        modelMap.put("paybean",paybean.get("data"));
        modelMap.put("title","订单支付");
		remoteService.qrCode(wxpay.get("openid"), account.getMobile(), account.getId());
        return "/order/orderPay";
    }


    @RequestMapping("/orderComment.do")
    public String orderComment(HttpServletRequest request,int orderId, ModelMap modelMap){
        OrderInfoService orderInfoService = new OrderInfoService();
        try {
            UserOrder order = orderInfoService.getOrderInfo(orderId);
            UserOrderItem userOrderItem = order.getGoods().get(0);
            modelMap.put("order",order);
            modelMap.put("orderInfo",userOrderItem);
            modelMap.put("title","我要评论");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/order/orderComment";
    }
    @RequestMapping("/orderComplaints.do")
    public String orderComplaints(HttpServletRequest request,int orderId, ModelMap modelMap){
        modelMap.put("orderId",orderId);
        modelMap.put("title","订单投诉");
        return "/order/orderComplaints";
    }
}
